﻿// JScript File
function ctlPageTool_init(ctlID) {
    var gridID = document.getElementById(ctlID + "_txtGridID").value;
    if (gridID == "") gridID = "mmmm111";
    var grd = document.getElementById(gridID);
    var txtCurrentPage = document.getElementById(ctlID + "_txtCurrentPage");
    var txtMaxPageNumber = document.getElementById(ctlID + "_txtMaxPageNumber");
    var txtMaxRecordNumber = document.getElementById(ctlID + "_txtMaxRecordNumber");
    var MaxRecordNumber = txtMaxRecordNumber.value;
    var MaxPageNumber = txtMaxPageNumber.value;
    var RecordsPerPage = document.getElementById(ctlID + "_txtRecordsPerPage").value;
    var refresh = document.getElementById(ctlID + "_txtRefresh").value;

    if (refresh == 0) return null;
    if (grd) {
        if (grd.rows) {
            MaxRecordNumber = grd.rows.length;
        }
        else {
            var i = 0;
            while (document.getElementById(gridID + "_" + i)) i++;
            MaxRecordNumber = i;
        }
        if (RecordsPerPage > 0) {
            MaxPageNumber = Math.ceil(MaxRecordNumber / RecordsPerPage);
            txtCurrentPage.value = 1;
            txtMaxRecordNumber.value = MaxRecordNumber;
            txtMaxPageNumber.value = MaxPageNumber;
        }
        else {
            txtCurrentPage.value = 0;
        }
    }
}

function ctlPageTool_load(ctlID) {
    try {
        ctlPageTool_init(ctlID);
        ctlPageTool_create(ctlID);
        ctlPageTool_showGridAndTable(ctlID);
    }
    catch (e) {
    }
}

function ctlPageTool_create(ctlID) {
    var ctl = document.getElementById(ctlID);
    var PagesDisplay = parseInt(document.getElementById(ctlID + "_txtPagesDisplay").value);
    var MaxPageNumber = parseInt(document.getElementById(ctlID + "_txtMaxPageNumber").value);
    var CurrentPage = parseInt(document.getElementById(ctlID + "_txtCurrentPage").value);
    var StylePageTool = parseInt(document.getElementById(ctlID + "_txtStylePageTool").value);
    var txtDau = document.getElementById(ctlID + "_txtDau").value;
    var txtCuoi = document.getElementById(ctlID + "_txtCuoi").value;
    var i;
    var p1 = (CurrentPage - Math.round((PagesDisplay - 1) / 2) > 0) ? (CurrentPage - Math.round((PagesDisplay - 1) / 2)) : (1);
    var p2;

    if (MaxPageNumber <= 1) {
        return null;
    }
    if (p1 + PagesDisplay - 1 < MaxPageNumber) {
        p2 = p1 + PagesDisplay - 1;
    }
    else {
        p2 = MaxPageNumber;
        p1 = (MaxPageNumber - PagesDisplay + 1 > 0) ? (MaxPageNumber - PagesDisplay + 1) : (1);
    }
    var strTG = "", strTG1, strTG2 = "<div class=\"space\"></div>";


    if (p1 > 1) {        
        switch (StylePageTool) {
            case 1:
                strTG1 = "<div id=\"" + ctlID + "_page_first\" class=\"btn_first\" onclick=\"ctlPageTool_selected('" + ctlID + "',-2)\"><span>" + txtDau + "</span></div>";
                strTG += strTG1 + strTG2;
                strTG1 = "<div id=\"" + ctlID + "_page_prev\" class=\"btn_prev\" onclick=\"ctlPageTool_selected('" + ctlID + "',-1)\"><span>&lt;</span></div>";
                strTG += strTG1 + strTG2;
                break;
            case 2:
            default:
                strTG1 = "<div id=\"" + ctlID + "_page_first\" class=\"btn_first\" onclick=\"ctlPageTool_selected('" + ctlID + "',-2)\"><span>&lt;&lt;</span></div>";
                strTG += strTG1 + strTG2;
                strTG1 = "<div id=\"" + ctlID + "_page_prev\" class=\"btn_prev\" onclick=\"ctlPageTool_selected('" + ctlID + "',-1)\"><span>&lt;</span></div>";
                strTG += strTG1 + strTG2;
                break;
        }
    }
    for (i = p1; i <= p2; i++) {
        if (i == CurrentPage)
            strTG1 = "<div id=\"" + ctlID + "_page_" + i + "\" class=\"btn_select\" onclick=\"ctlPageTool_selected('" + ctlID + "'," + i + ")\"><span> " + i + "</span></div>";
        else
            strTG1 = "<div id=\"" + ctlID + "_page_" + i + "\" class=\"btn\" onclick=\"ctlPageTool_selected('" + ctlID + "'," + i + ")\"><span> " + i + "</span></div>";
        if (i == p2)
            strTG += strTG1;
        else
            strTG += strTG1 + strTG2;
    }
    if (p2 < MaxPageNumber) {
        switch (StylePageTool) {
            case 1:
                strTG1 = "<div id=\"" + ctlID + "_page_next\" class=\"btn_next\" onclick=\"ctlPageTool_selected('" + ctlID + "',-3)\"><span>&gt;</span></div>";
                strTG += strTG2 + strTG1;
                strTG1 = "<div id=\"" + ctlID + "_page_last\" class=\"btn_last\" onclick=\"ctlPageTool_selected('" + ctlID + "',-4)\"><span>" + txtCuoi + "</span></div>";
                strTG += strTG2 + strTG1;
                break;
            case 2:
            default:
                strTG1 = "<div id=\"" + ctlID + "_page_next\" class=\"btn_next\" onclick=\"ctlPageTool_selected('" + ctlID + "',-3)\"><span>&gt;</span></div>";
                strTG += strTG2 + strTG1;
                strTG1 = "<div id=\"" + ctlID + "_page_last\" class=\"btn_last\" onclick=\"ctlPageTool_selected('" + ctlID + "',-4)\"><span>&gt;&gt;</span></div>";
                strTG += strTG2 + strTG1;
                break;
        }
    }
    ctl.className = "pagetool" + StylePageTool;
    ctl.innerHTML = strTG;
}

function ctlPageTool_selected(ctlID, PageNumber) {
    var txtCurrentPage = document.getElementById(ctlID + "_txtCurrentPage");
    var PagesDisplay = parseInt(document.getElementById(ctlID + "_txtPagesDisplay").value);
    var CurrentPage = parseInt(document.getElementById(ctlID + "_txtCurrentPage").value);
    var MaxPageNumber = parseInt(document.getElementById(ctlID + "_txtMaxPageNumber").value);
    var RunAtServer = parseInt(document.getElementById(ctlID + "_txtRunAtServer").value);
    var gt = CurrentPage;

    switch (PageNumber) {
        case -1: //Prev
            if (CurrentPage > 1) gt = CurrentPage - 1;
            break;
        case -2: //First
            gt = 1;
            break;
        case -3: //Next
            if (CurrentPage < MaxPageNumber) gt = CurrentPage + 1;
            break;
        case -4: //Last
            gt = MaxPageNumber;
            break;
        default:
            gt = PageNumber;
            break;
    }
    if (CurrentPage == gt) return null;
    //Bỏ nút cũ
    var itemID = ctlID + "_page_" + CurrentPage;
    var item = document.getElementById(itemID);
    item.className = "btn";
    //Chọn nút mới
    txtCurrentPage.value = gt;
    PageNumber = gt;
    if (RunAtServer) {
        var url = document.getElementById(ctlID + "_txtURL").value;
        if (url == "") {
            var btnPageSelected = document.getElementById(ctlID + "_btnPageSelected");
            btnPageSelected.click();
        }
        else {
            url = url.replace("#1#", PageNumber);
            document.location = url;
        }
    }
    else {
        ctlPageTool_showGridAndTable(ctlID);
        ctlPageTool_create(ctlID);
    }
}

function ctlPageTool_showGridAndTable(ctlID) {
    var RunAtServer = parseInt(document.getElementById(ctlID + "_txtRunAtServer").value);
    var RecordsPerPage = parseInt(document.getElementById(ctlID + "_txtRecordsPerPage").value);
    var CurrentPage = parseInt(document.getElementById(ctlID + "_txtCurrentPage").value);
    var MaxRecordNumber = parseInt(document.getElementById(ctlID + "_txtMaxRecordNumber").value);
    var gridID = document.getElementById(ctlID + "_txtGridID").value;
    if (gridID == "") gridID = "mmmm111";
    var grd = document.getElementById(gridID);
    var i, n, cs1, cs2;

    if (grd) {
        if (RunAtServer == 1) {
            n = RecordsPerPage;
            cs1 = 0;
            cs2 = n - 1;
        }
        else {
            n = MaxRecordNumber;
            cs1 = (CurrentPage - 1) * RecordsPerPage;
            cs2 = CurrentPage * RecordsPerPage - 1;
        }

        var item;
        for (i = 0; i <= n - 1; i++) {
            if (grd.rows) {
                if (i <= grd.rows.length - 1) {
                    item = grd.rows[i];
                }
                else {
                    item = null;
                }
            }
            else {
                item = document.getElementById(gridID + "_" + i);
            }
            if (item) {
                if (cs1 <= i && i <= cs2) {
                    item.style.display = '';
                }
                else {
                    if (item.style.display != 'none') {
                        item.style.display = 'none';
                    }
                }
            }
        }
        if (cs1 <= n) {
            try {
                var funcName = document.getElementById(ctlID + "_txtOnChange").value;
                window[funcName](gridID, cs1);
            }
            catch (err) {
            }
        }
    }
}